package com.Xht.www.servlet;
import com.Xht.www.dao.RoomTypeInfoDAO;
import com.Xht.www.po.RoomTypeInfoPO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.logging.Level;
import java.util.logging.Logger;

//后端接收添加房型请求

@WebServlet("/AddRoomServlet")
@MultipartConfig
public class AddRoomServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        // 获取参数
        String merchantName = request.getParameter("merchantName");
        String bedType = request.getParameter("bedType");
        String remainingQuantityStr = request.getParameter("remainingQuantity");
        String keywords = request.getParameter("keywords");
        String priceStr = request.getParameter("price");
        String salesVolumeStr = request.getParameter("salesVolume");

        // 打印接收到的参数（用于调试）
        System.out.println("接收到的参数：");
        System.out.println("merchantName: " + merchantName);
        System.out.println("bedType: " + bedType);
        System.out.println("remainingQuantityStr: " + remainingQuantityStr);
        System.out.println("keywords: " + keywords);
        System.out.println("priceStr: " + priceStr);
        System.out.println("salesVolumeStr: " + salesVolumeStr);

        // 分别检查每个参数是否为空
        if (merchantName == null || merchantName.isEmpty()) {
            System.err.println("merchantName 参数为空！");
            response.getWriter().println("请求参数不完整，请检查后重新提交！");
            return;
        }

        if (bedType == null || bedType.isEmpty()) {
            System.err.println("bedType 参数为空！");
            response.getWriter().println("请求参数不完整，请检查后重新提交！");
            return;
        }

        if (remainingQuantityStr == null || remainingQuantityStr.isEmpty()) {
            System.err.println("remainingQuantity 参数为空！");
            response.getWriter().println("请求参数不完整，请检查后重新提交！");
            return;
        }

        if (keywords == null || keywords.isEmpty()) {
            System.err.println("keywords 参数为空！");
            response.getWriter().println("请求参数不完整，请检查后重新提交！");
            return;
        }

        if (priceStr == null || priceStr.isEmpty()) {
            System.err.println("price 参数为空！");
            response.getWriter().println("请求参数不完整，请检查后重新提交！");
            return;
        }

        int remainingQuantity;
        BigDecimal price;
        int salesVolume = 0;

        try {
            remainingQuantity = Integer.parseInt(remainingQuantityStr);
            price = new BigDecimal(priceStr);
            if (salesVolumeStr != null && !salesVolumeStr.isEmpty()) {
                salesVolume = Integer.parseInt(salesVolumeStr);
            }
        } catch (NumberFormatException e) {
            System.err.println("参数格式错误：" + e.getMessage());
            response.getWriter().println("参数格式错误，请检查输入！");
            return;
        }

        // 打印即将插入的信息
        System.out.println("即将插入的房间信息：");
        System.out.println("商家名称: " + merchantName);
        System.out.println("床型: " + bedType);
        System.out.println("剩余数量: " + remainingQuantity);
        System.out.println("关键词: " + keywords);
        System.out.println("价格: " + price);
        System.out.println("销量: " + salesVolume);

        RoomTypeInfoPO roomTypeInfoPO = new RoomTypeInfoPO();
        roomTypeInfoPO.setMerchantName(merchantName);
        roomTypeInfoPO.setBedType(bedType);
        roomTypeInfoPO.setRemainingQuantity(remainingQuantity);
        roomTypeInfoPO.setKeywords(keywords);
        roomTypeInfoPO.setPrice(price);
        roomTypeInfoPO.setSalesVolume(salesVolume);

        RoomTypeInfoDAO roomTypeInfoDAO = new RoomTypeInfoDAO();
        try {
            roomTypeInfoDAO.insertRoomTypeInfo(roomTypeInfoPO);
            response.getWriter().println("房间信息添加成功！");
        } catch (Exception e) {
            Logger.getLogger(AddRoomServlet.class.getName()).log(Level.SEVERE, null, e);
            response.getWriter().println("房间信息添加失败，请检查数据库连接或其他设置！");
        }
    }
}
